package cn.edu.klmy.controller;

import cn.edu.klmy.dao.IScheduleDao;
import cn.edu.klmy.models.clasz.Clasz;
import cn.edu.klmy.models.param.SystemParam;
import cn.edu.klmy.models.schedule.CourseScheduling;
import cn.edu.klmy.models.schedule.SearchCourse;
import cn.edu.klmy.models.schedule.SearchTeacher;
import cn.edu.klmy.models.schedule.SituationCourseScheduling;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.List;

/**
 * @author 顾伟
 *         创建日期：13-4-21
 */
@Controller
@RequestMapping("/schedule")
public class ScheduleController {
    @Autowired
    private IScheduleDao scheduleDao;

    @RequestMapping("/list/all")
    @ResponseBody
    public List<CourseScheduling> loadAllCourseScheduling() {
        return scheduleDao.getAllCourseSchedule();
    }

    @RequestMapping("/list/current")
    @ResponseBody
    public List<SituationCourseScheduling> loadSituationCourseScheduling(HttpServletRequest request) {
        HttpSession session = request.getSession();
        SystemParam systemParam = (SystemParam) session.getAttribute("param");
        int term = systemParam.getTerm();
        int section = systemParam.getSection();

        return scheduleDao.getCurrentCourseSchedule(term,section);
    }

    @RequestMapping(value = "/list/current/conditions", method = RequestMethod.POST)
    @ResponseBody
    public List<SituationCourseScheduling> loadSituationCourseSchedulingByConditions(HttpServletRequest request,@RequestParam String courseName,@RequestParam String teacherName){
        HttpSession session = request.getSession();
        SystemParam systemParam = (SystemParam) session.getAttribute("param");
        int term = systemParam.getTerm();
        int section = systemParam.getSection();
        return scheduleDao.getCurrentCourseScheduleByConditions(term,section,courseName,teacherName);
    }

    @RequestMapping(value = "/list/conditions", method = RequestMethod.POST)
    @ResponseBody
    public List<CourseScheduling> loadCourseSchedulingByConditions(@RequestParam String courseName,@RequestParam String teacherName){
        return scheduleDao.getCourseScheduleByConditions(courseName,teacherName);
    }

    @RequestMapping("/search/course")
    @ResponseBody
    public List<SearchCourse> loadSearchCourse(){
         return scheduleDao.getSearchCourse();
    }

    @RequestMapping("/search/teacher")
    @ResponseBody
    public List<SearchTeacher> loadSearchTeacher(){
        return scheduleDao.getSearchTeacher();
    }

    @RequestMapping("/list/{id}/classes")
    @ResponseBody
    public List<Clasz> loadCourseSchedulingClasses(@PathVariable int id){
        return scheduleDao.getCourseScheduleClasses(id);
    }

    @RequestMapping("/notIn/{id}/list/class")
    @ResponseBody
    public List<Clasz> loadNonCourseSchedulingClasses(@PathVariable int id){
        return scheduleDao.getNonCourseScheduleClasses(id);
    }

    @RequestMapping("/{courseScheduleId}/class/delete/{classId}")
    @ResponseBody
    public boolean removeCourseSchedulingClass(@PathVariable int courseScheduleId,@PathVariable int classId){
         int deleteCount=scheduleDao.deleteCourseScheduleClass(courseScheduleId,classId);
        if(deleteCount!=0)
            return true;
        return false;
    }

    @RequestMapping("/{courseScheduleId}/class/add/{classId}")
    @ResponseBody
    public boolean addCourseSchedulingClass(@PathVariable int courseScheduleId,@PathVariable int classId){
        int addCount=scheduleDao.addCourseScheduleClass(courseScheduleId,classId);
        if(addCount!=0)
            return true;
        return false;
    }

    @RequestMapping("/add")
    @ResponseBody
    public int addCourseScheduling(@ModelAttribute CourseScheduling courseScheduling) {
        return scheduleDao.addCourseSchedule(courseScheduling);
    }

    @RequestMapping("/update")
    @ResponseBody
    public boolean updateCourseScheduling(@ModelAttribute CourseScheduling courseScheduling) {
        scheduleDao.updateCourseSchedule(courseScheduling);
        return true;
    }

    @RequestMapping("/delete/{id}")
    @ResponseBody
    public boolean removeCourseScheduling(@PathVariable int id) {
        scheduleDao.deleteCourseSchedule(id);
        return true;
    }
}
